package com.yunhe.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import com.yunhe.bean.ShoppingCarInfo;
import com.yunhe.inter.ShoppingCarInfoInter;
import com.yunhe.util.SqlSessionFactoryUtil;


@WebServlet("/ShoppingCarInfoServlet_2")
public class ShoppingCarInfoServlet_2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public ShoppingCarInfoServlet_2() {
        super();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 处理在购物车中点击删除事件
		 */
		//获取要删除的商品id
		Integer commodityID =  Integer.parseInt(request.getParameter("commodityID"));
		//获取数据库连接
		SqlSession sqlSession =SqlSessionFactoryUtil.openSqlSession();
		ShoppingCarInfoInter mapper = sqlSession.getMapper(ShoppingCarInfoInter.class);
		//执行删除操作
		mapper.delShoppingInfo(commodityID);
		sqlSession.commit();
		//再次加载数据
		ArrayList<ShoppingCarInfo> list  =mapper.findShoppingInfos();
		
		//再次将list集合放入request中
		request.setAttribute("list", list);
		request.getRequestDispatcher("/shopping.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
